package net.heri.xmppchat.activities.chatroom;

import net.heri.xmppchat.R;
import net.heri.xmppchat.persistence.MessageData;
import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.inject.Inject;

@ContentView(R.layout.chat_room_activity)
public class ChatRoomActivity extends RoboActivity implements OnClickListener {
	
	private final String TAG = "chatroom";
	
	@InjectView(R.id.edt_message) 
	private EditText			mEdtMessage;
	@InjectView(R.id.bt_send_message)
	private	Button				mBtnSendMessage;
	@InjectView(R.id.list_message) 
	private	ListView			mListMessage;
	@InjectView(R.id.txt_empty_message) 
	private	TextView			mTxtEmptyMessage;
	@Inject
	private ChatRoomController	mController;
	@Inject
	private ChatRoomAdapter		mAdapter;
	
	private IState				mState;
	
	private Handler mHandler = new Handler(){
		public void handleMessage(Message message) {
			switch(message.what) {
				case IChatRoomConstant.HandlerType.DISPLAY_TO_LIST : {
					
					if(mTxtEmptyMessage.getVisibility() == View.VISIBLE) {
						mTxtEmptyMessage.setVisibility(View.GONE);
					}
					
					mAdapter.addItem((MessageData) message.obj);
					mAdapter.notifyDataSetChanged();
					mListMessage.setSelection(mAdapter.getCount() - 1);
					
					break;
				}
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		mState = new StandbyState();
		mState.doAction();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.bt_send_message: {
				Log.i(TAG, "send message");
				
				mState = new SendMessageState();
				mState.doAction();
				
				break;
			}
		}
	}
	
	// ========================	//
	// INNER CLASS				//
	// ========================	//
	
	/**
	 *
	 */
	interface IState {
		void doAction();
	}
	
	/**
	 * 
	 */
	private class StandbyState implements IState {

		@Override
		public void doAction() {
			init();
		}
		
		private void init() {
			mController.setHandler(mHandler);
			mListMessage.setAdapter(mAdapter);
			mBtnSendMessage.setOnClickListener(ChatRoomActivity.this);
			
			mEdtMessage.setOnKeyListener(new OnKeyListener() {
				public boolean onKey(View v, int keyCode, KeyEvent event) {
					// If the event is a key-down event on the "enter" button
					if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
						mState = new SendMessageState();
						mState.doAction();
						return true;
					}
					return false;
				}
			});
		}
	}
	
	/**
	 * 
	 */
	private class SendMessageState implements IState {

		@Override
		public void doAction() {
			String message = mEdtMessage.getText().toString().trim();
			MessageData data = new MessageData();
			data.setBody(message);
			
			mEdtMessage.setText("");
			
			sendEvent(new IChatRoomEvent.OnSendMessage<MessageData>(data)  );
		}
		
		private void sendEvent(Object object) {
			ChatRoomActivity.this.eventManager.fire(object);
		}
	}
}
